import { defineConfig ,loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import autoprefixer from 'autoprefixer'
import WindiCSS from 'vite-plugin-windicss'
import { resolve } from 'path'
import legacy from '@vitejs/plugin-legacy'
import { viteMockServe } from 'vite-plugin-mock'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'

// https://vitejs.dev/config/
export default defineConfig(({ command,mode} )=>{
  let env = loadEnv(mode, process.cwd())
return{
  css: {
    postcss: {
      plugins: [
        autoprefixer({
          overrideBrowserslist: ['Chrome > 40', 'ff > 31', 'ie 11'],
        })
      ]
    },
    preprocessorOptions: {
      scss: {
        javascriptEnabled: true,
        additionalData: '@import "@/styles/variable.scss";',
      },
    },
  },
  plugins: [
    vue(),
    WindiCSS(),
    legacy({
      targets: ["cover 99.5%"],  
    }),
    viteMockServe({
      supportTs: false,
      logger: true,
      localEnabled: command ==='serve',
      mockPath:'./mock/',
      
    }),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    createSvgIconsPlugin({
      iconDirs: [resolve(process.cwd(),'src/assets/icons')],
      symbolId: 'icon-[dir]-[name]',
    }),
  ],
  resolve:{
    alias:[{
      find: '@',
      replacement: resolve(__dirname, './src')
    },
      ]
  },
  
  optimizeDeps: {
    include: [
      'core-js',
    ]
  },
  server:{
    proxy:{
      [env.VITE_APP_BASE_API]:{
        //获取数据的服务器地址
        target:env.VITE_SERVER_URL,
        //需要代理跨域
        changeOrigin:true,
        //路径重写
        rewrite:path=>{
          return path.replace(/^\/api/,'')
        }
      },
      [env.VITE_APP_BASE_API2]:{
        //获取数据的服务器地址
        target:env.VITE_SERVER_URL2,
        //需要代理跨域
        changeOrigin:true,
        //路径重写
        rewrite:path=>{
          return path.replace(/^\/server/,'')
        }
      }
    }
  }
}
})
